package com.recsmile.sgs.ui.components;

import com.recsmile.sgs.Constants;
import com.recsmile.sgs.entity.Card;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by Administrator on 2016/8/26.
 */
public class CardGui extends JButton{

    public static final int WIDTH = 142;
    public static final int HEIGHT = 200;

    private Card card;
    private String number;
    private Color color;
    private boolean known;
    private BufferedImage suit;
    private BufferedImage img;
    private BufferedImage img_darker;
    private float alpha = 1f;
    private boolean activated = false;

    public CardGui(Card card){
        this.card = card;
        setSize(WIDTH,HEIGHT);
        setRolloverEnabled(false);
        this.number = card.getNumber();
        readSuit(card.getSuit());
        readImage(card.getImage());
        known = true;
    }

    public CardGui() {
        setSize(WIDTH, HEIGHT);
        try {
            img = ImageIO.read(getClass().getResource("/cards/card_back.png"));
        } catch (IOException e) {
            System.err.println("File not found");
        }
        known = false;
    }

    private void readSuit(Card.Suit n) {
        try {
            switch (n) {
                case SPADE:
                    suit = ImageIO.read(getClass().getResource("/cards/spade.png"));
                    color = Color.BLACK;
                    break;
                case HEART:
                    suit = ImageIO.read(getClass().getResource("/cards/heart.png"));
                    color = Color.RED;
                    break;
                case CLUB:
                    suit = ImageIO.read(getClass().getResource("/cards/club.png"));
                    color = Color.BLACK;
                    break;
                case DIAMOND:
                    suit = ImageIO.read(getClass().getResource("/cards/diamond.png"));
                    color = Color.RED;
                    break;
            }
        } catch (IOException e) {
            System.err.println("File not found");
        }

    }


    private void readImage(String name) {
        try {
            img = ImageIO.read(getClass().getResource("cards/" + name + ".png"));
            img_darker = ImageIO.read(getClass().getResource("cards/" + name + "_darker.png"));
        } catch (IOException e) {
            System.err.println("File not found");
        } catch (Exception e) {
            System.err.println(name);
        }
    }


    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D graphics = (Graphics2D) g;
        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
        graphics.setComposite(ac);

        if (known) {
            if (isEnabled())
                graphics.drawImage(img, 0, 0, null);
            else
                graphics.drawImage(img_darker, 0, 0, null);
            graphics.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18));
            graphics.setColor(color);
            graphics.drawImage(suit, 10, 28, null);
            if (number.length() == 1)
                graphics.drawString(number, 15, 25);
            else
                graphics.drawString(number, 10, 25);
        } else {
            graphics.drawImage(img, 0, 0, null);
        }
        if (activated) {
            graphics.setColor(Color.RED);
            graphics.setStroke(new BasicStroke(Constants.BORDER_WIDTH));
            graphics.drawRect(0, 0, WIDTH, HEIGHT);
        }
    }

    public Card getCard() {
        return card;
    }

    protected void select() {
        activated = true;
        repaint();
    }

    protected void unselect() {
        activated = false;
        repaint();
    }

    protected void fade(float offset) {
        alpha -= offset;
        repaint();
    }
}
